﻿using System;
using System.Configuration;
using System.Security.Permissions;
using MakeItWeb.Framework;

namespace AllNations.WebAppAdmin
{
    public partial class EditContent : MakeItWebAdminPage
    {
        MakeItWeb.Framework.DatabaseContent m_oDatabaseContent = null;
        MakeItWeb.Framework.HtmlPage m_oReqHtmlPage = new MakeItWeb.Framework.HtmlPage();

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            if (string.IsNullOrEmpty(Request.QueryString["ContentCd"]))
                Response.Redirect("/Default.aspx", true);
            try
            {
                m_oDatabaseContent = MakeItWeb.Framework.DatabaseContent.GetDatabaseContentByCd(Request.QueryString["ContentCd"]);
                if (m_oDatabaseContent.HtmlPageId.Equals(new Guid(ConfigurationManager.AppSettings["GenericPageId"])))
                {
                    m_oReqHtmlPage = MakeItWeb.Framework.HtmlPage.GetPageDetail();
                }
                else if (m_oDatabaseContent.Loaded)
                {
                    m_oReqHtmlPage = MakeItWeb.Framework.HtmlPage.GetPageDetailById(m_oDatabaseContent.HtmlPageId);
                }
                else
                {
                    Guid guidHtmlPageId = new Guid(Request.QueryString["HtmlPageId"]);
                    m_oDatabaseContent = MakeItWeb.Framework.DatabaseContent.GetDatabaseContentByCd(Request.QueryString["ContentCd"], guidHtmlPageId);
                    m_oReqHtmlPage = MakeItWeb.Framework.HtmlPage.GetPageDetailById(guidHtmlPageId);
                }

                if (m_oReqHtmlPage.Loaded == false)
                    throw new ApplicationException("Content is not set up properly.");
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Framework.EditorHelper.InitializeEditor(TextEditor1, GetPageImagePath());
            if (!IsPostBack)
            {
                TextEditor1.Content = m_oDatabaseContent.ContentHtmlTxt;
            }
        }

        private string GetPageImagePath()
        {
            return string.Format("~/PageImages/{0}", m_oDatabaseContent.HtmlPageId);
        }

        protected void TextEditor1_SubmitClicked(object sender, EventArgs e)
        {
            m_oDatabaseContent.ContentTxt = TextEditor1.Text;
            m_oDatabaseContent.ContentHtmlTxt = TextEditor1.Content;
            m_oDatabaseContent.Save();

            string sCacheKey = string.Format("DatabaseContent_{0}", m_oDatabaseContent.ContentCd);
            Cache.Remove(sCacheKey);

            sCacheKey = string.Format("{0}_{1}", sCacheKey, m_oDatabaseContent.HtmlPageId);
            Cache.Remove(sCacheKey);

            RedirectToPage();
        }

        private void RedirectToPage()
        {
            if (m_oDatabaseContent.HtmlPageId.Equals(new Guid(ConfigurationManager.AppSettings["GenericPageId"])))
                Response.Redirect(string.Format("/Page.aspx?PageCd={0}", m_oDatabaseContent.ContentCd), true);
            else
                Response.Redirect(m_oReqHtmlPage.PageUrl, true);
        }

        protected void TextEditor1_CancelClicked(object sender, EventArgs e)
        {
            RedirectToPage();
        }
    }
}